<?php
 
 
class Compilador {

public $code;
public $resultadoCompilacion;

public function setCode($codeP)
{
	$this->code=$codeP;
}

public function getCode()
{
	return $this->code;
}

public function setResultadoCompilacion($result)
{
	$this->resultadoCompilacion=$result;
}

public function getResultadoCompilacion()
{
	return $this->resultadoCompilacion;
}


public function compilar($codigoP,$nombreP)
{
	
	$this->setCode(str_replace('\n','',$codigoP));//$codigo=str_replace('\n','',$codigoP);	
	$cur_dir = dirname(__FILE__);
	$filename=$cur_dir.'/../servicios/'.$nombreP.".php";	
	$codeFile=fopen($filename,"w+");
	fwrite($codeFile,"<?php \n");
	fwrite($codeFile,'include_once "include_service.php"'.";\n");
	
	fwrite($codeFile,stripslashes($this->getCode()));
	fwrite($codeFile,"?>");
	fflush($codeFile);
	
	define('OK_LINE_START','No syntax errors detected in');

	include_once "compilador_config.php";
	$cmd = "$command $filename";	
	exec($cmd,$output,$result);
	
	$len = count($output);
	if ( $len <= 0 ) {
		echo "Compilador Mal Configurado";
		exit(1);
	}
	
	// finally parse output of syntax check
	
	if ( substr($output[0],0,strlen(OK_LINE_START)) == OK_LINE_START ) {
		$syntax_OK = true;
		$this->setResultadoCompilacion(true);
		return true;
	} else {
	$this->setResultadoCompilacion(false);
	return false; 
	}	
	}
}

 
?>
